# 商品API测试脚本
Write-Host "========================================" -ForegroundColor Green
Write-Host "商品API测试" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""

# 测试函数
function Test-API {
    param(
        [string]$Url,
        [string]$Description
    )
    
    Write-Host "测试: $Description" -ForegroundColor Yellow
    Write-Host "URL: $Url" -ForegroundColor Gray
    
    try {
        $response = Invoke-RestMethod -Uri $Url -Method GET -TimeoutSec 10
        Write-Host "[成功]" -ForegroundColor Green
        $response | ConvertTo-Json -Depth 3 | Write-Host -ForegroundColor Cyan
        return $true
    }
    catch {
        Write-Host "[失败] $($_.Exception.Message)" -ForegroundColor Red
        return $false
    }
    finally {
        Write-Host ""
    }
}

# 1. 检查商品服务健康状态
Write-Host "1. 检查商品服务健康状态..." -ForegroundColor Blue
$healthResult = Test-API -Url "http://localhost:5233/health" -Description "商品服务健康检查"

if (-not $healthResult) {
    Write-Host "商品服务未运行，请先启动商品服务" -ForegroundColor Red
    Write-Host "启动命令: dotnet run --project hao-cloud-api\hao-Code\4.Api\hao-Product.Api" -ForegroundColor Yellow
    Read-Host "按Enter键退出"
    exit 1
}

# 2. 测试商品列表API
Write-Host "2. 测试商品列表API..." -ForegroundColor Blue
$listResult = Test-API -Url "http://localhost:5233/api/product/list?page=1&pageSize=5" -Description "获取商品列表"

# 3. 测试商品详情API（使用一个测试ID）
Write-Host "3. 测试商品详情API..." -ForegroundColor Blue
$detailResult = Test-API -Url "http://localhost:5233/api/product/00000000-0000-0000-0000-000000000001" -Description "获取商品详情"

# 4. 测试Swagger文档
Write-Host "4. 检查Swagger文档..." -ForegroundColor Blue
try {
    $swaggerResponse = Invoke-WebRequest -Uri "http://localhost:5233/swagger" -Method GET -TimeoutSec 5
    if ($swaggerResponse.StatusCode -eq 200) {
        Write-Host "[成功] Swagger文档可访问" -ForegroundColor Green
        Write-Host "访问地址: http://localhost:5233/swagger" -ForegroundColor Cyan
    }
}
catch {
    Write-Host "[失败] Swagger文档不可访问: $($_.Exception.Message)" -ForegroundColor Red
}
Write-Host ""

# 5. 测试结果汇总
Write-Host "========================================" -ForegroundColor Green
Write-Host "测试结果汇总" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green

$results = @(
    @{Name="健康检查"; Result=$healthResult},
    @{Name="商品列表"; Result=$listResult},
    @{Name="商品详情"; Result=$detailResult}
)

$passedTests = 0
$totalTests = $results.Count

foreach ($result in $results) {
    $status = if ($result.Result) { "✅ 通过" } else { "❌ 失败" }
    $color = if ($result.Result) { "Green" } else { "Red" }
    
    Write-Host "$($result.Name): $status" -ForegroundColor $color
    
    if ($result.Result) {
        $passedTests++
    }
}

Write-Host ""
Write-Host "总计: $passedTests/$totalTests 测试通过" -ForegroundColor $(if ($passedTests -eq $totalTests) { "Green" } else { "Red" })

if ($passedTests -eq $totalTests) {
    Write-Host ""
    Write-Host "🎉 所有测试通过！商品API工作正常" -ForegroundColor Green
    Write-Host "现在可以在移动端测试商品展示功能" -ForegroundColor Green
} else {
    Write-Host ""
    Write-Host "⚠️  部分测试失败，请检查：" -ForegroundColor Yellow
    Write-Host "- 确保商品服务正在运行" -ForegroundColor Yellow
    Write-Host "- 检查数据库连接" -ForegroundColor Yellow
    Write-Host "- 查看服务日志排查问题" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "📋 API接口说明：" -ForegroundColor Cyan
Write-Host "- GET /health - 健康检查" -ForegroundColor Gray
Write-Host "- GET /api/product/list - 商品列表" -ForegroundColor Gray
Write-Host "- GET /api/product/{id} - 商品详情" -ForegroundColor Gray
Write-Host "- GET /swagger - API文档" -ForegroundColor Gray

Write-Host ""
Read-Host "Press Enter to exit"